package fr.afcepf.al22.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.sql.DataSource;

import fr.afcepf.al22.qualite.entity.Personne;
import fr.afcepf.al22.util.DataSourceGestionPersonneQualite;

/**
 * dao personne pour interagir avec la base.
 * 
 * @author Stagiaire
 */
public class DaoPersonneImpl implements DaoPersonne {
    /**
     * datasource.
     */
    private DataSource ds = new DataSourceGestionPersonneQualite();
    /**
     * requete d'ajout.
     */
    private final String reqAjout = "INSERT INTO personne (nom, motdepasse, mail,date_naissance) VALUES (?,?,?,?)";

    private final int indicenom = 1;
    private final int indicemail = 2;
    private final int indicemdp = 3;
    private final int indicenaissance = 4;

    @Override
    public Personne ajouter(Personne paramPers) {
	Connection cnx = null;
	try {
	    cnx = ds.getConnection();
	    
	    System.out.println();
	    PreparedStatement pstmt = cnx.prepareStatement(reqAjout,
		    Statement.RETURN_GENERATED_KEYS);
	    pstmt.setString(indicenom, paramPers.getNom());
	    pstmt.setString(indicemail, paramPers.getMail());
	    pstmt.setString(indicemdp, paramPers.getMotdepasse());
	    pstmt.setDate(indicenaissance, new java.sql.Date(paramPers
		    .getDatenaissance().getTime()));

	    int nbLigne = pstmt.executeUpdate();

	    ResultSet rs = pstmt.getGeneratedKeys();

	    while (nbLigne == 1 && rs.next()) {

		paramPers.setIdPersonne(rs.getInt(1));

	    }
	    System.out.println("dao paramPers ==> " + paramPers);

	} catch (SQLException e) {

	    e.printStackTrace();
	} finally {
	    try {
		cnx.close();
	    } catch (SQLException e) {

		e.printStackTrace();
	    }
	}


	return paramPers;
    }

    @Override
    public List<Personne> find(String paramName) {
	// TODO Auto-generated method stub
	return null;
    }

    @Override
    public Personne verifMail(String paramMail) {
	// TODO Auto-generated method stub
	return null;
    }

}
